package com.captain.special.二叉树;

import com.captain.leetcode.TreeNode;

import java.util.*;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/9 0:01
 */
public class 二叉树的层序遍历II107 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        ArrayList<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> queue = new ArrayDeque<>();
        //初始化
        if (root != null) {
            queue.add(root);
        }
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> tp = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node != null) {
                    tp.add(node.val);
                    if (node.left != null) queue.add(node.left);
                    if (node.right != null) queue.add(node.right);
                }
            }
            res.add(tp);
        }
        Collections.reverse(res);
        return res;
    }
}
